
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>什么是Java 的 happens-before规则？ - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>什么是Java 的 happens-before规则？ - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">Java 内存模型中的 `happens-before` 规则主要解决了什么问题？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`happens-before` 规则用来定义多线程程序中操作之间的可见性和排序规则。它规定了一个线程的操作如何在另一个线程中可见，从而确保程序在并发环境下能够正确执行。</div>
          </div>
          <div class="card-source">来源: Java 的 happens-before 规则</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">规则</div>
          <div class="card-question">请解释 `happens-before` 的“锁规则”是如何保证线程间内存可见性的。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">规则</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">一个 unlock 操作 `happens-before` 其后的 lock 操作。这意味着，如果线程 A 释放了一个锁，而线程 B 随后获取了同一个锁，那么线程 A 在释放锁之前执行的所有操作，对线程 B 都是可见的。</div>
          </div>
          <div class="card-source">来源: 锁规则</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">规则</div>
          <div class="card-question">对 `volatile` 变量的读写操作遵循怎样的 `happens-before` 规则？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">规则</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">对 `volatile` 变量的写操作 `happens-before` 其后的任何对该变量的读操作。这保证了当一个线程读取 `volatile` 变量时，总能看到最新的写入值。</div>
          </div>
          <div class="card-source">来源: volatile 变量规则</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">规则</div>
          <div class="card-question">在一个单线程内部，操作的执行顺序遵循什么 `happens-before` 规则？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">规则</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">在同一个线程中，操作的顺序与代码的顺序相同，这被称为“程序顺序规则”。例如，`x = 1;` 在代码中先于 `y = 2;`，那么 `x = 1;` 的操作就 `happens-before` `y = 2;` 的操作。</div>
          </div>
          <div class="card-source">来源: 程序顺序规则</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">规则</div>
          <div class="card-question">线程的启动和结束是如何与 `happens-before` 规则关联的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">规则</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当线程 A 启动线程 B 时，线程 A 对共享变量的操作 `happens-before` 线程 B 中的任何操作。当线程 B 执行完毕后，线程 B 的所有操作 `happens-before` 线程 A 中的任何后续操作。</div>
          </div>
          <div class="card-source">来源: 线程启动和结束规则</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">应用</div>
          <div class="card-question">理解 `happens-before` 规则在实际编程中有什么重要意义？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">应用</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">它帮助开发者设计正确的并发程序，确保线程间通信的正确性，避免数据竞争和不可预测的行为。在实现线程安全的单例模式、并发队列或设计锁机制时，`happens-before` 规则是至关重要的基础。</div>
          </div>
          <div class="card-source">来源: 在实际应用中的意义</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
